﻿using Business;
using Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace MVP
{
    public class AktivasiPresenter : BasePresenter<IAktivasiView>
    {
        private IAktivasiView _view;
        private AktivasiBusiness _business
        {
            get { return (AktivasiBusiness)Business; }
        }
        public AktivasiPresenter(IAktivasiView view)
            : base(view)
        {
            _view = view;
            Business = new AktivasiBusiness();
        }

        protected override void OnViewInitialize(object sender, EventArgs e)
        {
            base.OnViewInitialize(sender, e);
            Initialize();
        }

        private void Initialize()
        {
            DataTable dt = _business.LoadData();
            if (dt.Rows.Count > 0)
            {
                long productNumber = Convert.ToInt64(dt.Rows[0]["product_number"]);
                long activationCode = Convert.ToInt64(dt.Rows[0]["activation_code"]);
                string macAddress = dt.Rows[0]["mac_address"].ToString();

                bool isValid = _business.ValidateActivationCode(macAddress, productNumber, activationCode);
                if (isValid)
                {
                    _view.ProductNumber = productNumber.ToString();
                    _view.ActivationCode = activationCode.ToString();
                }
                else 
                {
                    _view.ProductNumber = GuardManager.GetSerialNumber().ToString();
                }
            }
            else
            {
                _view.ProductNumber = GuardManager.GetSerialNumber().ToString();
            }
        }

        public bool Activate(string productNumber, string activationCode)
        {
            if (_business.ValidateActivationCode(Convert.ToInt64(productNumber), Convert.ToInt64(activationCode)))
            {
                _view.SendMessageInfo("Aktivasi berhasil. Terima kasih.");
                return true;
            }
            else
            {
                _view.SendMessageInfo("Activation Code tidak valid.");
                return false;
            }
        }

        
    }
}
